﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace 无线打分网站
{
    [Serializable]
    public class TeachGradeData
    {

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string SHIYANField = "@ShiYan";

        public const string TEACHERField = "@Teacher";

        public const string GRADEField = "@Grade";

        public const string ROOMField = "@Room";

        public const string ZUOHAOField = "@ZuoHao";

        public const string DATETIMEField = "@Datetime";

        public const string REMARKField = "@Remark";

        private Guid _ID;

        private String _ShiYan;

        private String _Teacher;

        private Int32 _Grade;

        private String _Room;

        private Int32 _ZuoHao;

        private DateTime _Datetime;

        private String _Remark;

        public Guid ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String ShiYan
        {
            get { return _ShiYan; }
            set { _ShiYan = value; }
        }
        public String Teacher
        {
            get { return _Teacher; }
            set { _Teacher = value; }
        }
        public Int32 Grade
        {
            get { return _Grade; }
            set { _Grade = value; }
        }
        public String Room
        {
            get { return _Room; }
            set { _Room = value; }
        }
        public Int32 ZuoHao
        {
            get { return _ZuoHao; }
            set { _ZuoHao = value; }
        }
        public DateTime Datetime
        {
            get { return _Datetime; }
            set { _Datetime = value; }
        }
        public String Remark
        {
            get { return _Remark; }
            set { _Remark = value; }
        }
        public TeachGradeData()
        {
            _ID = Guid.Empty;
            _ShiYan = "";
            _Teacher = "";
            _Grade = 0;
            _Room = "";
            _ZuoHao = 0;
            _Datetime = DateTime.Now;
            _Remark = "";
        }
    }

    public class TeachGradeDataFactory
    {
        public static TeachGradeData Construct(IDataReader reader)
        {
            TeachGradeData data = new TeachGradeData();

            try { data.ID = (Guid)(reader["ID"]); }
            catch { }

            try { data.ShiYan = Convert.ToString(reader["ShiYan"]); }
            catch { }

            try { data.Teacher = Convert.ToString(reader["Teacher"]); }
            catch { }

            try { data.Grade = Convert.ToInt32(reader["Grade"]); }
            catch { }

            try { data.Room = Convert.ToString(reader["Room"]); }
            catch { }

            try { data.ZuoHao = Convert.ToInt32(reader["ZuoHao"]); }
            catch { }

            try { data.Datetime = Convert.ToDateTime(reader["Datetime"]); }
            catch { }

            try { data.Remark = Convert.ToString(reader["Remark"]); }
            catch { }

            return data;
        }
        public static Guid GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Guid GetPrimaryKey(DataTable dt, int rowIndex)
        {
            Guid PrimaryKey = Guid.Empty;

            if (dt.Rows.Count < rowIndex + 1)

                return Guid.Empty;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return Guid.Empty;

            try { PrimaryKey = (Guid)(dr[TeachGradeData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static TeachGradeData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static TeachGradeData Construct(DataTable dt, int rowIndex)
        {
            TeachGradeData data = new TeachGradeData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.ShiYan = Convert.ToString(dr["ShiYan"]); }
            catch { }

            try { data.Teacher = Convert.ToString(dr["Teacher"]); }
            catch { }

            try { data.Grade = Convert.ToInt32(dr["Grade"]); }
            catch { }

            try { data.Room = Convert.ToString(dr["Room"]); }
            catch { }

            try { data.ZuoHao = Convert.ToInt32(dr["ZuoHao"]); }
            catch { }

            try { data.Datetime = Convert.ToDateTime(dr["Datetime"]); }
            catch { }

            try { data.Remark = Convert.ToString(dr["Remark"]); }
            catch { }

            return data;
        }
        public static TeachGradeData Construct(DataRow dr)
        {
            TeachGradeData data = new TeachGradeData();

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.ShiYan = Convert.ToString(dr["ShiYan"]); }
            catch { }

            try { data.Teacher = Convert.ToString(dr["Teacher"]); }
            catch { }

            try { data.Grade = Convert.ToInt32(dr["Grade"]); }
            catch { }

            try { data.Room = Convert.ToString(dr["Room"]); }
            catch { }

            try { data.ZuoHao = Convert.ToInt32(dr["ZuoHao"]); }
            catch { }

            try { data.Datetime = Convert.ToDateTime(dr["Datetime"]); }
            catch { }

            try { data.Remark = Convert.ToString(dr["Remark"]); }
            catch { }

            return data;
        }
        public static Guid GetPrimaryKey(DataRow dr)
        {
            Guid PrimaryKey = Guid.Empty;

            try { PrimaryKey = (Guid)(dr[TeachGradeData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class TeachGradeDataDBOption
    {
        public static int Insert(TeachGradeData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into TeachGrade (");
            sql.Append("ShiYan, ");
            sql.Append("Teacher, ");
            sql.Append("Grade, ");
            sql.Append("Room, ");
            sql.Append("ZuoHao, ");
            sql.Append("Datetime, ");
            sql.Append("Remark");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.ShiYan.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Teacher.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.Grade.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Room.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.ZuoHao.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Datetime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Remark.ToString());
            sql.Append("'");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(TeachGradeData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update TeachGrade set ");
            sql.Append("ShiYan=");
            sql.Append("'");
            sql.Append(d.ShiYan.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Teacher=");
            sql.Append("'");
            sql.Append(d.Teacher.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Grade=");
            sql.Append("");
            sql.Append(d.Grade.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("Room=");
            sql.Append("'");
            sql.Append(d.Room.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("ZuoHao=");
            sql.Append("");
            sql.Append(d.ZuoHao.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("Datetime=");
            sql.Append("'");
            sql.Append(d.Datetime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Remark=");
            sql.Append("'");
            sql.Append(d.Remark.ToString());
            sql.Append("'");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(TeachGradeData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from TeachGrade ");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from TeachGrade ");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataTable DataTableSelect(TeachGradeData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from TeachGrade where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from TeachGrade ");
            return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
        }
        public static SqlDataReader ReaderSelect(TeachGradeData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from TeachGrade ");
            return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
        }
        public static bool CheckResubmit(string ex,int zuohao,int t1,int t2)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from TeachGrade where (ShiYan = '");
            sql.Append(ex);
            sql.Append("') AND (ZuoHao = ");
            sql.Append(zuohao.ToString());
            sql.Append(") AND (Datetime BETWEEN '");
            sql.Append(DateTime.Now.ToString("yyyy-MM-dd"));
            sql.Append(" ");
            sql.Append((t1 / 100).ToString("D2"));
            sql.Append(":");
            sql.Append((t1 % 100).ToString("D2"));
            sql.Append(":00' AND '");
            sql.Append(DateTime.Now.ToString("yyyy-MM-dd"));
            sql.Append(" ");
            sql.Append((t2 / 100).ToString("D2"));
            sql.Append(":");
            sql.Append((t2 % 100).ToString("D2"));
            sql.Append(":00')");
            DataTable dt= WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
            if(dt.Rows.Count==0)
                return false;
            else
                return true;
        }
    }
}